import Cookies from 'js-cookie'
import 'normalize.css/normalize.css' // A modern alternative to CSS resets
import Vue from 'vue'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// import locale from 'element-ui/lib/locale/lang/zh-CN' // lang i18n
import YlwlUI from 'ylwl-ui'
import 'ylwl-ui/lib/index/style.css'

import '@/styles/index.scss' // global css

import App from './App'
import router from './router'
import store from './store'

import '@/icons' // icon
import '@/permission' // permission control

import SliderVerificationCode from 'slider-verification-code'
import 'slider-verification-code/lib/slider-verification-code.css'
Vue.use(SliderVerificationCode)

import VueParticles from 'vue-particles'
Vue.use(VueParticles)

import '../theme/e-index.css'
import actions from './action'
import request from './utils/request'
Vue.prototype.$request = request
Vue.use(ElementUI)
Vue.use(YlwlUI)

import moment from 'moment'
Vue.prototype.$moment = moment

Vue.config.productionTip = false

// 权限框架注册全局
import permission from '@/directive/permission/index.js' // 权限判断指令
import waves from '@/directive/waves/index.js' // 波纹
Vue.use(permission).use(waves)

// new Vue({
//   el: '#app',
//   router,
//   store,
//   render: (h) => h(App)
// })
let instance = null
function render() {
  // console.log(props.permission, '子应用store+++++++++++')
  // router = new VueRouter({
  //   base: window.__POWERED_BY_QIANKUN__ ? '/vue-app-one' : '/',
  //   mode: 'history',
  //   routes
  // })

  instance = new Vue({
    router,
    store,
    render: (h) => h(App)
  }).$mount('#app')
}

// 单独启动时候直接执行渲染
window.__POWERED_BY_QIANKUN__ || render()

export async function bootstrap(props = {}) {
  // if (props.fns && props.fns.length > 0) {
  //   props.fns.map((i) => {
  //     Vue.prototype[i.name] = i[i.name]
  //   })
  // }
  // // 子应用内部拿到监听器并绑定到vue实例，子应用若想要发送消息需要使用this.$subscriber.next({from:'', ...})
  // if (props.keyname) {
  //   window.localStorage.setItem('micro_customer_keyname', props.keyname)
  // }
  // debugger
  console.log('vue起动')
  console.log('接收到父应用传递来的数据:', props)
}

export async function mount(props) {
  // actions.setActions(props)
  console.log('props 打开', props)
  Vue.prototype.$microMode = true
  actions.setActions(props)

  // const fullPath = window.localStorage.getItem('micro_customer_keyname')
  // // 直接重定向
  // if (fullPath) {
  //   router.push(fullPath)
  // }

  if (store.getters.sidebar.opened != Cookies.get('sidebarStatus')) {
    store.dispatch('app/toggleSideBar')
  }

  render()
  // if (window.mainAppLoading) {
  //   // debugger
  //   window.mainAppLoading(false)
  // }
}

export async function unmount() {
  console.log('props 关闭')
  instance.$destroy()
  instance = null
}
